package me.zhengjie.modules.member.rest;

import com.qiaofang.applet.service.member.service.EmployeeReportService;
import com.qiaofang.applet.service.member.service.dto.InteractReportDto;
import com.qiaofang.applet.service.member.service.dto.OperateReportDto;
import com.qiaofang.applet.service.member.service.dto.ShareEmpReportDto;
import com.qiaofang.applet.service.member.service.dto.ShareHouseReportDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.member.vo.EmpReportQueryVo;
import me.zhengjie.modules.quartz.task.EmployeeReportCreateTask;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "报表：数据报表")
@RequestMapping("/api/report")
public class ReportController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ReportController.class);

    @Autowired
    private EmployeeReportService employeeReportService;

    @Autowired
    private EmployeeReportCreateTask employeeReportCreateTask;

    @Autowired
    private UserService userService;

    @GetMapping("/getShareHouseReport")
    @ApiOperation(value = "房源分享报表", notes = "房源分享报表")
    public ResponseEntity<Page<ShareHouseReportDto>> getShareHouseReport(EmpReportQueryVo queryVo, Pageable pageable) {
        OperateReportDto reportDto = new OperateReportDto();
        BeanUtils.copyProperties(queryVo, reportDto);
        reportDto.setCorporationCode(SecurityUtils.getCorporationCode());
        reportDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        return ResponseEntity.ok(employeeReportService.getShareHouseReport(reportDto, pageable));
    }

    @GetMapping("/getShareEmpReport")
    @ApiOperation(value = "名片分享报表", notes = "名片分享报表")
    public ResponseEntity<Page<ShareEmpReportDto>> getShareEmpReport(EmpReportQueryVo queryVo, Pageable pageable) {
        OperateReportDto reportDto = new OperateReportDto();
        BeanUtils.copyProperties(queryVo, reportDto);
        reportDto.setCorporationCode(SecurityUtils.getCorporationCode());
        reportDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        return ResponseEntity.ok(employeeReportService.getShareEmpReport(reportDto, pageable));
    }

    @GetMapping("/getInteractReport")
    @ApiOperation(value = "互动报表", notes = "互动报表")
    public ResponseEntity<Page<InteractReportDto>> getInteractReport(EmpReportQueryVo queryVo, Pageable pageable) {
        OperateReportDto reportDto = new OperateReportDto();
        BeanUtils.copyProperties(queryVo, reportDto);
        reportDto.setCorporationCode(SecurityUtils.getCorporationCode());
        reportDto.setCityCodes(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        return ResponseEntity.ok(employeeReportService.getInteractReport(reportDto, pageable));
    }

    @GetMapping("/brushHistory")
    @ApiOperation(value = "刷历史数据", notes = "刷历史数据")
    @AnonymousAccess
    public ResponseEntity<Object> brushHistory(@RequestParam("dateStr") String dateStr) {
        employeeReportCreateTask.reportProcess(dateStr);
        return ResponseEntity.ok(HttpStatus.OK);
    }
}